﻿(function () {
    var _inited = false;
    var _handler = "../Handlers/ModuleInfoHandler.ashx";
    var _subhandler = "../Handlers/SubModuleInfoHandler.ashx";

    var SubModuleInfoMgr = {
        getSubModuleInfo: function (params) {
            SubModuleInfoMgr.InitSubUI();
            if (params && params != null) {
                $.ajax({
                    type: "get",
                    url: _subhandler + "?action=GetSubModuleInfo&Params=" + encodeURIComponent($.toJSON(params.AutoID)) + "&ts=" + new Date().valueOf(),
                    dataType: 'json',
                    success: function (data, textStatus) {
                        ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        if (data != null && data.length > 0) {
                            for (var i = 0; i < data.length; i++) {
                                var d = data[i];
                                var tr = '<tr>';
                                tr += '<td>';
                                tr += d.DisplayID;
                                tr += '</td>';
                                tr += '<td>';
                                tr += d.ModuleSubName;
                                tr += '</td>';
                                tr += '<td>';
                                tr += d.CreateTime;
                                tr += '</td>';
                                tr += '<td>';
                                tr += d.CreateFlag == 1 ? '已初始化' : '未初始化';
                                tr += '</td>';
                                tr += '</tr>';
                                $('#tbsubmoduleinfo tbody').append($(tr).data('data', d));
                            }
                        }
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
                    }
                });
            }
        },
        addSubModuleInfo: function (params) {
            if (params && params != null) {
                $.ajax({
                    type: "post",
                    url: _subhandler,
                    data: "action=AddSubModuleInfo&Params=" + encodeURIComponent($.toJSON(params)),
                    dataType: 'json',
                    success: function (data, textStatus) {
                        ProcessAjaxResult(data, textStatus,
                        function (data, textStatus) {
                            SubModuleInfoMgr.HandlerMsg(data);
                        },
                        function (err, textStatus) {
                            alert('操作失败');
                        },
                        this);
                    }
                });
            }
        },
        delSubModuleInfo: function (params) {
            if (params && params != null) {
                $.ajax({
                    type: "post",
                    url: _subhandler,
                    data: "action=DelSubModuleInfo&Params=" + encodeURIComponent($.toJSON(params)),
                    dataType: 'json',
                    success: function (data, textStatus) {
                        ProcessAjaxResult(data, textStatus,
                        function (data, textStatus) {
                            SubModuleInfoMgr.HandlerMsg(data);
                        },
                        function (err, textStatus) {
                            alert('操作失败');
                        },
                        this);
                    }
                });
            }
        },
        saveSubModuleInfo: function (params) {
            if (params && params != null) {
                $.ajax({
                    type: "post",
                    url: _subhandler,
                    data: "action=SaveSubModuleInfo&Params=" + encodeURIComponent($.toJSON(params)),
                    dataType: 'json',
                    success: function (data, textStatus) {
                        ProcessAjaxResult(data, textStatus,
                        function (data, textStatus) {
                            SubModuleInfoMgr.HandlerMsg(data);
                        },
                        function (err, textStatus) {
                            alert('操作失败');
                        },
                        this);
                    }
                });
            }
        },
        createSubModuleInfo: function (params) {
            if (params && params != null) {
                $.ajax({
                    type: "post",
                    url: _subhandler,
                    data: "action=CreateSubModuleInfo&Params=" + encodeURIComponent($.toJSON(params)),
                    dataType: 'json',
                    success: function (data, textStatus) {
                        ProcessAjaxResult(data, textStatus,
                        function (data, textStatus) {
                            switch (data) {
                                case 0:
                                    alert('操作成功');
                                    break;
                                case 100:
                                    alert('已经存在数据');
                                    break;
                                case 200:
                                    alert('没有字段');
                                    break;
                                default:
                                    alert('操作失败');
                                    break;
                            }
                        },
                        function (err, textStatus) {
                            alert('操作失败');
                        },
                        this);
                    }
                });
            }
        },
        HandlerMsg: function (msg) {
            switch (msg) {
                case 0:
                    alert('操作成功');
                    SubModuleInfoMgr.InitSubData();
                    break;
                case 100:
                    alert('子模块存在');
                    break;
                default:
                    alert('操作失败');
                    break;
            }
        },
        InitSubData: function () {
            var mt = $('#tbmoduleinfo').data('data');
            SubModuleInfoMgr.getSubModuleInfo(mt);
        },
        InitSubUI: function () {
            $('#tbsubmoduleinfo tbody').html('');
            $('#tbsubmoduleinfo').data('data', null);
            $('#tbsubmoduleinfo tbody tr').die().live('click', function () {
                $('#tbsubmoduleinfo tbody tr').removeClass('active');
                $(this).addClass('active');
                var info = $(this).data('data');
                $('#tbsubmoduleinfo').data('data', info);
            });
            $('#tbsubmoduleinfo thead').die().live('click', function () {
                $('#tbsubmoduleinfo tbody tr').removeClass('active');
                $('#tbsubmoduleinfo').data('data', null);
            });
        }
    };

    function _init() {
        _initUI();
        _initDetailUI();
        SubModuleInfoMgr.getSubModuleInfo();
        _getData();
    }

    function _handlerMsg(msg) {
        switch (msg) {
            case 0:
                alert('操作成功');
                _init();
                break;
            case 100:
                alert('模块存在');
                break;
            default:
                alert('操作失败');
                break;
        }
    }

    function _initUI() {
        $('#tbmoduleinfo tbody').html('');
        $('#tbmoduleinfo tbody tr').die().live('click', function () {
            $('#tbmoduleinfo tbody tr').removeClass('active');
            $(this).addClass('active');
            var info = $(this).data('data');
            _initDetailUI(info);
            SubModuleInfoMgr.getSubModuleInfo(info);
        });
        $('#tbmoduleinfo thead').die().live('click', function () {
            $('#tbmoduleinfo tbody tr').removeClass('active');
            _initDetailUI();
            SubModuleInfoMgr.getSubModuleInfo();
        });
        $('#btnModuleImport', 'table.tbhead').die().live('click', function () {
            var cb = function (params) {
                if (params != null) {
                    _importData(params.AutoID);   
                }
            };
            top.IndexScript.ModuleImportDialog.Open(null, cb);
        });
        $('#btnModuleAdd', 'table.tbhead').die().live('click', function () {
            var cb = function (params) {
                _addData(params);
            };
            top.IndexScript.ModuleDialog.Open(null, cb);
        });
        $('#btnModuleDelete', 'table.tbhead').die().live('click', function () {
            var mi = $('#tbmoduleinfo').data('data');
            if (mi && mi != null) {
                if (confirm('确定删除该数据吗？')) {
                    _delData(mi);
                }
            }
            else {
                alert('请选择一项主功能模块');
            }
        });
        $('#btnModuleModify', 'table.tbhead').die().live('click', function () {
            var mi = $('#tbmoduleinfo').data('data');
            if (mi && mi != null) {
                var cb = function (params) {
                    _saveData(params);
                };
                top.IndexScript.ModuleDialog.Open(mi, cb);
            }
            else {
                alert('请选择一项主功能模块');
            }
        });
        $('#btnSubModuleAdd', 'table.tbhead').die().live('click', function () {
            var info = $('#tbmoduleinfo').data('data');
            if (info && info != null) {
                var cb = function (params) {
                    //指定主模块ID
                    var mt = $('#tbmoduleinfo').data('data');
                    if (mt != null) {
                        if (params != null) {
                            params.ModuleID = mt.AutoID;
                        }
                        SubModuleInfoMgr.addSubModuleInfo(params);
                    }
                };
                top.IndexScript.SubModuleDialog.Open(null, cb);
            }
            else {
                alert('请选择一项主功能模块');
            }
        });
        $('#btnSubModuleDelete', 'table.tbhead').die().live('click', function () {
            var mi = $('#tbsubmoduleinfo').data('data');
            if (mi && mi != null) {
                if (confirm('确定删除该数据吗？')) {
                    SubModuleInfoMgr.delSubModuleInfo(mi);
                }
            }
            else {
                alert('请选择一项子功能模块');
            }
        });
        $('#btnSubModuleModify', 'table.tbhead').die().live('click', function () {
            var mi = $('#tbsubmoduleinfo').data('data');
            if (mi && mi != null) {
                var cb = function (params) {
                    SubModuleInfoMgr.saveSubModuleInfo(params);
                };
                top.IndexScript.SubModuleDialog.Open(mi, cb);
            }
            else {
                alert('请选择一项子功能模块');
            }
        });
        $('#btnSubModuleSetting', 'table.tbhead').die().live('click', function () {
            var mi = $('#tbsubmoduleinfo').data('data');
            if (mi && mi != null) {
                //关闭子模块设置窗口刷新本页面
                var cb = function () {
                    SubModuleInfoMgr.InitSubData();
                };
                top.IndexScript.SubFieldDialog.Open(mi, cb);
            }
            else {
                alert('请选择一项子功能模块');
            }
        });
        $('#btnSubModuleCreate', 'table.tbhead').die().live('click', function () {
            var mi = $('#tbsubmoduleinfo').data('data');
            if (mi && mi != null) {
                if (mi.CreateFlag == 1) {
                    alert('表已经初始化');
                    return;
                }
                if (confirm('初始化后将无法再修改，确定吗？')) {
                    SubModuleInfoMgr.createSubModuleInfo(mi);
                }
            }
            else {
                alert('请选择一项子功能模块');
            }
        });
    }

    function _initDetailUI(cd) {
        if (cd && cd != null) {
            $('#tbmoduleinfo').data('data', cd);
        }
        else {
            $('#tbmoduleinfo').data('data', null);
        }
    }

    function _getData() {
        $.ajax({
            type: "get",
            url: _handler + "?action=GetModuleInfo" + "&ts=" + new Date().valueOf(),
            dataType: 'json',
            async: false,
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _bindData(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _bindData(data) {
        if (data) {
            for (var i = 0; i < data.length; i++) {
                var d = data[i];
                var tr = '<tr>';
                tr += '<td>';
                tr += d.DisplayID;
                tr += '</td>';
                tr += '<td>';
                tr += d.ModuleName;
                tr += '</td>';
                tr += '<td>';
                tr += d.CreateTime;
                tr += '</td>';
                tr += '</tr>';
                $('#tbmoduleinfo tbody').append($(tr).data('data', d));
            }
        }
    }

    function _importData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=ImportModule&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _addData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=AddModuleInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _delData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=DelModuleInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    function _saveData(params) {
        $.ajax({
            type: "post",
            url: _handler,
            data: "action=SaveModuleInfo&Params=" + encodeURIComponent($.toJSON(params)),
            dataType: 'json',
            success: function (data, textStatus) {
                ProcessAjaxResult(data, textStatus,
                    function (data, textStatus) {
                        _handlerMsg(data);
                    },
                    function (err, textStatus) {
                        alert('操作失败');
                    },
                    this);
            }
        });
    }

    this.ModuleInfoMgr = {
        init:
        function () {
            if (!!_inited) return;
            _init();
            _inited = true;
        }
    }
})();
$(function () {
    ModuleInfoMgr.init();
});